﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using Phoenix.Common.Helper;

namespace Phoenix.Common.Util
{
	public static class HtmlHelperExtensions
	{
		#region HeadLink

		public static string HeadLink(this HtmlHelper htmlHelper, string rel, string href, string type, string title)
		{
			return htmlHelper.HeadLink(rel, href, type, title, null);
		}

		public static string HeadLink(this HtmlHelper htmlHelper, string rel, string href, string type, string title, object htmlAttributes)
		{
			TagBuilder tagBuilder = new TagBuilder("link");

			tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
			if (!string.IsNullOrEmpty(rel))
			{
				tagBuilder.MergeAttribute("rel", rel);
			}
			if (!string.IsNullOrEmpty(href))
			{
				// 增加版本号
				href += "?v=" + ConfigHelper.Settings.StaticResourceVersion;
				tagBuilder.MergeAttribute("href", href);
			}
			if (!string.IsNullOrEmpty(type))
			{
				tagBuilder.MergeAttribute("type", type);
			}
			if (!string.IsNullOrEmpty(title))
			{
				tagBuilder.MergeAttribute("title", title);
			}

			return tagBuilder.ToString(TagRenderMode.SelfClosing);
		}

		#endregion

		#region RenderCssFile

		public static void RenderCssFile(this HtmlHelper htmlHelper, string path)
		{
			if (string.IsNullOrEmpty(path))
			{
				throw new ArgumentNullException("css file path not null");
			}

			htmlHelper.ViewContext.HttpContext.Response.Write(htmlHelper.HeadLink("stylesheet", path, "text/css", ""));
		}

        public static void Css(this HtmlHelper htmlHelper, string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("css file path not null");
            }

            htmlHelper.ViewContext.HttpContext.Response.Write(htmlHelper.HeadLink("stylesheet", ConfigHelper.Settings.StaticsUrl + path, "text/css", ""));
        }

		#endregion

		#region RenderScriptTag

		public static void RenderScriptFile(this HtmlHelper htmlHelper, string path)
		{
			if (string.IsNullOrEmpty(path))
			{
				throw new ArgumentNullException("script file path not null");
			}

			htmlHelper.ViewContext.HttpContext.Response.Write(htmlHelper.ScriptBlock("text/javascript", path));
		}

        public static void Script(this HtmlHelper htmlHelper, string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("script file path not null");
            }

            htmlHelper.ViewContext.HttpContext.Response.Write(htmlHelper.ScriptBlock("text/javascript", ConfigHelper.Settings.StaticsUrl + path));
        }

		public static void RenderScriptFile(this HtmlHelper htmlHelper, string path, object htmlAttribute)
		{
			if (string.IsNullOrEmpty(path))
			{
				throw new ArgumentNullException("script file path not null");
			}
			if (htmlAttribute == null)
			{
				throw new ArgumentNullException("htmlAttribute not null");
			}


			htmlHelper.ViewContext.HttpContext.Response.Write(htmlHelper.ScriptBlock("text/javascript", path, htmlAttribute));
		}

		#endregion


		#region ScriptBlock

		public static string ScriptBlock(this HtmlHelper htmlHelper, string type, string src)
		{
			return htmlHelper.ScriptBlock(type, src, null);
		}

		public static string ScriptBlock(this HtmlHelper htmlHelper, string type, string src, object htmlAttributes)
		{
			TagBuilder tagBuilder = new TagBuilder("script");

			tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
			if (!string.IsNullOrEmpty(type))
			{
				tagBuilder.MergeAttribute("type", type);
			}
			if (!string.IsNullOrEmpty(src))
			{
				src += "?v=" + ConfigHelper.Settings.StaticResourceVersion;
				tagBuilder.MergeAttribute("src", src);
			}

			return tagBuilder.ToString(TagRenderMode.Normal);
		}

		#endregion


		#region Json扩展方法（允许Get请求获取json）

		//public static ShowResult XJson(this Controller controller, Object data)
		//{
		//    ShowResult result = new ShowResult();
		//    result.Data = data;
		//    result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

		//    return result;
		//}

		#endregion
	}

}
